﻿//-----------------------------------------------------------------------
// <copyright file="CartController.cs" company="上海Zoomla!逐浪CMS团队">
// 版权所有:🏢江西逐浪软件科技有限公司  🏅🏅🏅20年专注研发==>商城|ERP|字库|高端门户与人工智能 
// Website:🚀www.z01.com 
// Contact:☎ 13177777714  😎 wechat: zoomla 
// Effect: 🔧 购物车控制器  
// Create: ⏰2020年09月17日,06:18
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities;
using SportsStore.WebUI.Models;

namespace SportsStore.WebUI.Controllers
{
    public class CartController : Controller
    {
        private IProductsRepository repository;
        private IOrderProcessor orderProcessor;

        public CartController(IProductsRepository repo, IOrderProcessor proc)
        {
            repository = repo;
            orderProcessor = proc;
        }

        public CartController(IProductsRepository repo)
        {
            repository = repo;
        }

        /// <summary>
        /// 调用CartIndexViewModel的index方法，此方法用于创造购物车视图 by 逐浪cms发哥 2020年09月17日,06:24
        /// </summary>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        //public ViewResult Index(string returnUrl)
        public ViewResult Index(Cart cart, string returnUrl)
        {
            return View(new CartIndexViewModel
            {
                ReturnUrl = returnUrl,
                Cart = cart
            });
        }

        public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl)
        {
            Product product = repository.Products
            .FirstOrDefault(p => p.ProductID == productId);
            if (product != null)
            {
                //GetCart().AddItem(product, 1);
                cart.AddItem(product, 1);
            }
            return RedirectToAction("Index", new { returnUrl });
        }

        public RedirectToRouteResult RemoveFromCart(Cart cart, int productId, string returnUrl)
        {
            Product product = repository.Products
            .FirstOrDefault(p => p.ProductID == productId);
            if (product != null)
            {
                //GetCart().RemoveLine(product);
                cart.RemoveLine(product);
            }
            return RedirectToAction("Index", new { returnUrl });
        }

        private Cart GetCart()
        {
            Cart cart = (Cart)Session["Cart"];
            if (cart == null)
            {
                cart = new Cart();
                Session["Cart"] = cart;
            }
            return cart;
        }

        /// <summary>
        /// 购物车摘要控制器
        /// </summary>
        /// <param name="cart"></param>
        /// <returns></returns>
        public PartialViewResult Summary(Cart cart)
        {
            return PartialView(cart);
        }
        /// <summary>
        /// 购物车结算按钮
        /// </summary>
        /// <returns></returns>
        public ViewResult Checkout()
        {
            return View(new ShippingDetails());
        }
        /// <summary>
        /// 完成购物车
        /// </summary>
        /// <param name="cart"></param>
        /// <param name="ShippingDetails"></param>
        /// <returns></returns>
        [HttpPost]
        public ViewResult Checkout(Cart cart, ShippingDetails ShippingDetails)
        {
            if (cart.lines.Count() == 0)
            {
                ModelState.AddModelError("", "Sorryg,your cart is empty!购物车是空的");
            }
            if (ModelState.IsValid)
            {
                orderProcessor.ProcessOrder(cart, ShippingDetails);
                cart.Clear();
                return View("Completed");
            }
            else
            {
                return View(ShippingDetails);
            }
        }
    }
}